/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"template/server/http/global"
	"template/server/http/pkg/initialize"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"go.uber.org/zap"
)

// serverCmd represents the server command
var serverCmd = &cobra.Command{
	Use:   "server",
	Short: "A brief description of your command",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	RunE: ServerRun,
}
var configFilePath string

func init() {
	serverCmd.Flags().StringVarP(&configFilePath, "config", "c", "", "config file path")
	viper.BindPFlag("config", serverCmd.Flags().Lookup("config"))
	serverCmd.MarkFlagRequired("config")
	rootCmd.AddCommand(serverCmd)
}
func ServerRun(cmd *cobra.Command, args []string) (err error) {
	err = initialize.ReadConfig(configFilePath)
	if err != nil {
		return err
	}
	initialize.InitLogger()
	r := initialize.Router()
	zap.L().Sugar().Info("server start")
	err = r.Run(fmt.Sprintf("%s:%d", global.Config.Server.ListenHostAddr, global.Config.Server.ListenHostPort))
	if err != nil {
		return err
	}
	return nil
}
